/*  Copyright (c) 2011, 2012 MIEM (http://miem.edu.ru/)
    All rights reserved.

    Authors:
        Andrey Shtykovskiy (ashty@itas.miem.edu.ru)

    This file is part of LEAQ (Linguistic Engineering AQuarium) software.

    Redistribution and use in source and binary forms, with or without modification, are
    permitted provided that the following conditions are met:

       1. Redistributions of source code must retain the above copyright notice, this list of
          conditions and the following disclaimer.

       2. Redistributions in binary form must reproduce the above copyright notice, this list
          of conditions and the following disclaimer in the documentation and/or other materials
          provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
    SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __LEAQ_KERNEL_PLUGINMANAGER_H__
#define __LEAQ_KERNEL_PLUGINMANAGER_H__

#include <QObject>
#include <QDebug>
#include <QDir>
#include <QLinkedList>
#include <QStringList>
#include <QPluginLoader>

namespace leaq {
namespace kernel {

template<typename IPluginType>
class PluginManager
{
public:
    PluginManager() { }
    ~PluginManager() { /* @todo add cleanup */ }

    void start()
    {
        // a two-pass procedure

        // 1. initialize all plugins
        foreach(QPluginLoader* loader, pluginLoaders_) {
            pluginFrom(loader)->initialize();
        }

        // 2. activate all plugins
        foreach(QPluginLoader* loader, pluginLoaders_) {
            pluginFrom(loader)->activate();
        }
    }

    void load(QString path)
    {
        const QDir pluginsDir(path);
        QStringList fileNameList = pluginsDir.entryList();

        foreach(QString fileName, fileNameList) {
            tryLoad(pluginsDir.absoluteFilePath(fileName));
        }
    }

    void load(QStringList pathList)
    {
        foreach(QString path, pathList) {
            tryLoad(path);
        }
    }

private:
    void tryLoad(QString fileFullPath)
    {
        QPluginLoader* loader = new QPluginLoader(fileFullPath, &anchor);

        QObject* plugin = loader->instance();

        if(isValid(plugin)) {
            // a plugin can be unloaded using
            // loader only --> storing loader
            pluginLoaders_.push_back(loader);

            qDebug() << "got valid entry";
            qDebug() << fileFullPath;
        }
    }

    bool isValid(QObject* plugin)
    {
        if(!plugin) {
            return false;
        }

        IPluginType* currentPlugin =
                qobject_cast<IPluginType*>(plugin);

        if(!currentPlugin) {
            return false;
        }

        foreach(QPluginLoader* loader, pluginLoaders_) {
            IPluginType* plugin = pluginFrom(loader);

            if(currentPlugin->info().name == plugin->info().name) {
                return false;
            }
        }

        return true;
    }

    IPluginType* pluginFrom(QPluginLoader* loader)
    {
        return qobject_cast<IPluginType*>(loader->instance());
    }

    QObject anchor;
    QLinkedList<QPluginLoader*> pluginLoaders_;
};

} // namespace kernel
} // namespace leaq

#endif // __LEAQ_KERNEL_PLUGINMANAGER_H__
